import { getAjax, deleteAjax } from '@/utils/request'
import { Mutation, Action } from 'vuex'

export interface Logininfor {
  infoId: number;
  userName: string;
  status: string;
  ipaddr: string;
  loginLocation: string;
  browser: string;
  os: string;
  msg: string;
  loginTime: Date;
  beginTime: Date;
  endTime: Date;
}

export interface StateType {
  data: Logininfor;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_DATA: Mutation<StateType>;
  };
  actions: {
    getList: Action<StateType, StateType>;
    deleteLog: Action<StateType, StateType>;
    clean: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    data: {
      infoId: -1,
      userName: '',
      status: '',
      ipaddr: '',
      loginLocation: '',
      browser: '',
      os: '',
      msg: '',
      loginTime: new Date(),
      beginTime: new Date(),
      endTime: new Date()
    }
  },
  mutations: {
    SET_DATA: (state, data) => {
      state.data = data
    }
  },
  actions: {
    getList(_, data) {
      return getAjax('/monitor/logininfor/list', data)
    },
    deleteLog(_, id) {
      return deleteAjax(`/monitor/logininfor/${id}`, null)
    },
    clean(_) {
      return deleteAjax('/monitor/logininfor/clean', null)
    }
  }
}

export default storeModel
